clean_if_bad_names <- function(x) {
if (any(str_detect(names(x), "[\\sA-Z()/-]"))) {
clean_names(x)
}
x
}
# reset df
df <- penguins_raw
clean_if_bad_names(x = df)
## # A tibble: 344 × 17
## studyName `Sample Number` Species Region Island Stage `Individual ID`
## <chr> <dbl> <chr> <chr> <chr> <chr> <chr>
## 1 PAL0708 1 Adelie Pengu… Anvers Torge… Adult,… N1A1
## 2 PAL0708 2 Adelie Pengu… Anvers Torge… Adult,… N1A2
## 3 PAL0708 3 Adelie Pengu… Anvers Torge… Adult,… N2A1
## 4 PAL0708 4 Adelie Pengu… Anvers Torge… Adult,… N2A2
## 5 PAL0708 5 Adelie Pengu… Anvers Torge… Adult,… N3A1
## 6 PAL0708 6 Adelie Pengu… Anvers Torge… Adult,… N3A2
## 7 PAL0708 7 Adelie Pengu… Anvers Torge… Adult,… N4A1
## 8 PAL0708 8 Adelie Pengu… Anvers Torge… Adult,… N4A2
## 9 PAL0708 9 Adelie Pengu… Anvers Torge… Adult,… N5A1
## 10 PAL0708 10 Adelie Pengu… Anvers Torge… Adult,… N5A2
## # … with 334 more rows, and 10 more variables: Clutch Completion <chr>,
## # Date Egg <date>, Culmen Length (mm) <dbl>, Culmen Depth (mm) <dbl>,
## # Flipper Length (mm) <dbl>, Body Mass (g) <dbl>, Sex <chr>,
## # Delta 15 N (o/oo) <dbl>, Delta 13 C (o/oo) <dbl>, Comments <chr>
clean_names() from janitor looks for more bad elements in names
How do we know?
Pro tip: cmd-click on the function name below
janitor::clean_names()
Select the data.frame method from the drop down
Then cmd click the internal function make_clean_names()
Now we see the internals on janitor’s make_clean_names(), we see it cleans up apostrophes and other things our function didn’t.
dplyr::arrange()
In the chunk above, the options in the gear icon do not provide a way for:
{r echo=TRUE, eval=FALSE}
which means to show the code but do not run the code. Am I missing something?
Have you seen people post these cool images of their code:
The carbonate package makes those.
We are going to combine some carbonate functions into one function using purrr::compose()
# compose the set of carbonate functions to open your copied code at the carbonate URL
snip <- purrr::compose(
~ my_code$browse(),
~ assign(x$template, "one-dark", pos = ".GlobalEnv"),
~ assign("my_code", carbon$new(), pos = ".GlobalEnv"),
~ library(carbonate)
)
(will not work if your system does not have the xclip or xsel utility)
snip()
More manually if you cannot grab code from your clipboard
x <- carbon$new(
'
clean_if_bad_names <- function(x) {
if (any(str_detect(names(x), "[\\sA-Z()/-]"))) {
clean_names(x)
}
}
'
)
# available templates
x$get_templates()
# chose one and send to browser
x$template <- "one-dark"
x$browse()
Pro tip: alt+up or alt+down to move lines
Pro tip: ctl+alt+down or ctl+alt+up to have multi-line cursor
You try: add commas after starts_with() and mean
df %>%
summarise(
across(
starts_with("culmen")
mean
na.rm = TRUE
)
)
Pro tip: double click one of the parentheses to highlight what it contains
div(
class = "outer",
div(
class = "container",
div(class = "data",
df %>%
filter(
str_detect(Species, "Chin"),
Sex == "MALE",
Island == "Dream"
) %>%
summarise(
across(
starts_with("culmen"),
mean,
na.rm = TRUE
)
)
)
)
)
Comment and not
Pro tip: highlight multiple lines of code, shift-ctl-c to comment all